package com.icoo.apigateway.system.exceptionConfig;

import com.icoo.base.model.ResultModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常控制类
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 404异常处理
	 */
	@ExceptionHandler(value = NoHandlerFoundException.class)
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public ResultModel errorHandler(NoHandlerFoundException exception) {
		log.error("路径不存在，请检查路径是否正确",exception);
		return ResultModel.fail("路径不存在，请检查路径是否正确",HttpStatus.NOT_FOUND.value());
	}

	/**
	 * 405异常处理
	 */
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public ResultModel errorHandler(HttpRequestMethodNotSupportedException exception) {
		log.error(exception.getMessage(),exception);
		return ResultModel.fail(exception.getMessage(),HttpStatus.METHOD_NOT_ALLOWED.value());
	}

	/**
	 * 415异常处理
	 */
	@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
	public ResultModel errorHandler( HttpMediaTypeNotSupportedException exception) {
		log.error(exception.getMessage(),exception);
		return ResultModel.fail(exception.getMessage(),HttpStatus.UNSUPPORTED_MEDIA_TYPE.value());
	}


	@ExceptionHandler(RuntimeException.class)
	public ResultModel handleRuntimeException(RuntimeException exception) {
		log.error("系统异常，操作失败",exception);
		return ResultModel.fail("系统异常，操作失败");
	}

	/**
	 * 空指针异常
	 */
	@ExceptionHandler(NullPointerException.class)
	public ResultModel nullPointerExceptionHandler(NullPointerException exception) {
		log.error("空指针异常!",exception);
		return ResultModel.fail("空指针异常!");
	}

	/**
	 * 类型转换异常
	 */
	@ExceptionHandler(ClassCastException.class)
	public ResultModel classCastExceptionHandler(ClassCastException exception) {
		log.error("类型转换异常!",exception);
		return ResultModel.fail("类型转换异常!");
	}

	/**
	 * 数组越界异常
	 */
	@ExceptionHandler(ArrayIndexOutOfBoundsException.class)
	public ResultModel ArrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException exception) {
		log.error("数组越界异常!",exception);
		return ResultModel.fail("数组越界异常!");
	}

	/**
	 * 表单验证异常处理
	 */
	@ExceptionHandler(value = BindException.class)
	public ResultModel validExceptionHandler(BindException exception, HttpServletRequest request, HttpServletResponse response) {
		FieldError fieldError = exception.getBindingResult().getFieldError();
		log.error(fieldError.getDefaultMessage(),exception);
		return ResultModel.fail(fieldError.getDefaultMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value());
	}

	/**
	 * 500异常处理
	 */
	@ExceptionHandler(value = Exception.class)
	public ResultModel errorHandler (HttpServletRequest request, Exception exception, HttpServletResponse response) {
		log.error("系统异常!",exception);
		return ResultModel.fail("系统异常!",HttpStatus.INTERNAL_SERVER_ERROR.value());
	}

}
